/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Logica.Accion;
import Logica.Comentario;
import Persistencia.ADOAccion;
import javax.swing.JOptionPane;

/**
 *
 * @author NachoB
 */
public class IActualizarAccion extends javax.swing.JFrame {
    
    ADOAccion ado=new ADOAccion();
    private int codAccion;
    private String nombreAccion;
    private int estado;
    private Accion actAccion = new Accion ();
           
    public String getCodAccion() {
        return Integer.toString(codAccion);
    }
    public String getNombreAccion() {
        return nombreAccion;
    }
    public int getEstado(){
        return estado;
    }
    
    public void setCodAccion(int codAccion) {
        this.codAccion = codAccion;
        txtIdAccion.setText(Integer.toString(codAccion));
    }
    public void setNombreAccion(String nombreAccion) {
        this.nombreAccion = nombreAccion;
        txtNombre.setText(nombreAccion);
    }
    public void setEstado(int estado){
        this.estado = estado;
        cbEstado.setSelectedIndex(estado);    
    }
    
    public IActualizarAccion() {
        initComponents();       
               
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblIdAccion = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        txtIdAccion = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        lblComentario = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaComentario = new javax.swing.JTextArea();
        lblEstado = new javax.swing.JLabel();
        cbEstado = new javax.swing.JComboBox();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        lblResultadoComentario = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtaColeccion = new javax.swing.JTextArea();
        lblComentariosAgregados = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblIdAccion.setText("id_accion:");

        lblNombre.setText("Nombre:");

        txtIdAccion.setEnabled(false);

        txtNombre.setEnabled(false);

        lblComentario.setText("Comentario:");

        txtaComentario.setColumns(20);
        txtaComentario.setRows(5);
        jScrollPane1.setViewportView(txtaComentario);

        lblEstado.setText("Estado:");

        cbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cerrada", "Abierta", "Vencida" }));

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAgregar.setText("Agregar Comentario");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        txtaColeccion.setColumns(20);
        txtaColeccion.setRows(5);
        txtaColeccion.setEnabled(false);
        jScrollPane2.setViewportView(txtaColeccion);

        lblComentariosAgregados.setText("Comentarios Agregados:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnCancelar)
                                    .addGap(28, 28, 28)
                                    .addComponent(btnGuardar))
                                .addComponent(btnAgregar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblIdAccion)
                                    .addComponent(lblNombre)
                                    .addComponent(lblEstado))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNombre)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtIdAccion, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(cbEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(26, 26, 26))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblComentario)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblComentariosAgregados)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(76, 76, 76))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblResultadoComentario, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIdAccion)
                    .addComponent(txtIdAccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEstado))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblComentario)
                    .addComponent(lblComentariosAgregados))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblResultadoComentario, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(btnAgregar)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addGap(19, 19, 19))
        );

        lblComentariosAgregados.getAccessibleContext().setAccessibleName("Comentarios Agregados:");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        //Cierra la ventana actual limpiando el comentario del label, el textArea de comentarios y la lista de comentarios en accion (mediante el objeto creado con su ID).
        try{
        //Accion actAccion = new Accion ();        
        actAccion.setId(Integer.parseInt(txtIdAccion.getText()));
        actAccion.limpiarLista();
        }catch(Exception e){};
        lblResultadoComentario.setText("");
        txtaComentario.setText("");
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        //metodo que actualiza el estado de las accioens y añade comentarios a la misma
        //Un try para capturar excepciones en el momento de guardar comentarios y actualizar estado
        try{
           //Creo objeto Accion y le doy los valores del ID (para reconocer la accion) y Estado (para actualizarlo)
           //Accion actAccion = new Accion ();
           actAccion.setId(Integer.parseInt(txtIdAccion.getText()));
           actAccion.setEstado(cbEstado.getSelectedIndex());           
           //Con el objeto ADOAccion llamo al metodo que actualizara el estado pasando la Accion antes creada
           ado.actualizarAccion(actAccion); 
           JOptionPane.showMessageDialog(null, "Accion Actualizada con éxito.");
           //try que evita colisiones cuando no se cargaron comentarios y se desea solo cambiar el estado.
           try{
               actAccion.limpiarLista();
           }catch(Exception e){};
           this.dispose();
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, "Error no se inserto");
            e.printStackTrace();
        
        }
        lblResultadoComentario.setText("");
    }//GEN-LAST:event_btnGuardarActionPerformed
    
    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        //Este metodo permite agregar comentarios a una lista de comentarios en la clase accion, los cuales luego podran ser añadidos al guardar la transaccion.
        
        //Valido si el text Area contiene texto.
        if(txtaComentario.getText().equals(""))
        {
            lblResultadoComentario.setText("Complete el Area de comentario antes de agregar");
        }
        else
        {   
            //Comentario comenta= new Comentario (txtaComentario.getText(), Integer.parseInt(txtIdAccion.getText()));
            //Accion actAccion = new Accion (Integer.parseInt(txtIdAccion.getText()));
            Comentario comenta= new Comentario ();
            comenta.setIdAccion(Integer.parseInt(txtIdAccion.getText()));
            comenta.setDescripcion(txtaComentario.getText());
            //Accion actAccion = new Accion ();
            actAccion.setId(Integer.parseInt(txtIdAccion.getText()));
            if(actAccion.addComentario(comenta))
            {                
                txtaColeccion.setText(actAccion.mostrarListaComentario());
                //txtaColeccion.setText(txtaColeccion.getText()+ "\n" + txtaComentario.getText());
                lblResultadoComentario.setText("¡Comentario agregado con exito!");
                txtaComentario.setText("");
            }
            else
                lblResultadoComentario.setText("No se pudo agregar el comentario.");
            
        }
        
        
    }//GEN-LAST:event_btnAgregarActionPerformed
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cbEstado;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblComentario;
    private javax.swing.JLabel lblComentariosAgregados;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblIdAccion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblResultadoComentario;
    private javax.swing.JTextField txtIdAccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextArea txtaColeccion;
    private javax.swing.JTextArea txtaComentario;
    // End of variables declaration//GEN-END:variables



}
